package org.ycy.data.generator.core;

import org.ycy.data.generator.constants.Symbol;
import org.ycy.data.generator.entity.Column;
import org.ycy.data.generator.enums.GeneratorType;
import org.ycy.data.generator.generator.AutoIncrementGenerator;
import org.ycy.data.generator.generator.IGenerator;
import org.ycy.data.generator.generator.PollingGenerator;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GeneratorFactory {

    private static Map<String, IGenerator> generatorMap = new ConcurrentHashMap<>();


    public static IGenerator buildGenerator(String name, Column column) {

        String key = name + Symbol.UNDER_LINE + column.getColumnCode();
        if (!generatorMap.containsKey(key)) {

            IGenerator generator = null;
            GeneratorType generatorType = column.getGeneratorType();
            switch (generatorType) {
                case RANDOM:
                    generator = column.getValueType().buildRandomGenerator();
                    break;
                case POLLING:
                    generator = new PollingGenerator();
                    break;
                case AUTOINCREMENT:
                    generator = new AutoIncrementGenerator();
                    break;

            }
            generator.setCheckInfo(column.getCheckInfo());
            generatorMap.put(key, generator);
        }

        return generatorMap.get(key);

    }

}
